package org.nobject.common.swing.g;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JScrollPane;

/**
 * GFillLayout
 *
 * @author bianrongjun
 * @version 1.0
 */
public class GFillLayout extends GLayout{
	
	/** 纵向均匀分布 */
	public final static int TYPE_VERTICAL=0;
	/** 横向均匀分布 */
	public final static int TYPE_HORIZAL=1;
	/** 完全填充 */
	public final static int TYPE_FULL=-1;
	/** 类型 */
	public int type=TYPE_FULL;
	
	/**
	 * 构造函数 
	 */
	public GFillLayout(){
		
	}
	
	/**
	 * 构造函数 
	 * @param type
	 */
	public GFillLayout(int type){
		this.type=type;
	}
	
	/* (non-Javadoc)
	 * @see org.nobject.common.swing.g.GLayout#addLayoutComponent(java.lang.String, java.awt.Component)
	 */
	public void addLayoutComponent(String name, Component comp) {
	}
	
	/**
	 * keysize
	 */
	private Set keysize=new HashSet();
	
	/**
	 * @param c
	 */
	public void setKeysize(Component c){keysize.add(c);}
	
	/* (non-Javadoc)
	 * @see org.nobject.common.swing.g.GLayout#layoutContainer(java.awt.Container)
	 */
	public void layoutContainer(Container parent) {
		Component [] childs=parent.getComponents();
		if(childs.length==0) return;
		int h=0;
		int w=0;
		int x=0;
		int y=0;
		
		//已配宽度的总宽
		int certainWidth=0;
		
		//未分配宽度的量
		int uncertainCount=0;
		for (int i = 0; i < childs.length; i++){
			if(keysize.contains(childs[i])) continue;
			if(childs[i].getWidth()>0) certainWidth+=childs[i].getWidth();
			else uncertainCount++;
		}
		
		//未分配宽度的平均宽度
		int generalPerWidth=0;
		if(uncertainCount>0&parent.getWidth()>certainWidth)  generalPerWidth=(parent.getWidth()-certainWidth)/uncertainCount;
		for (int i = 0; i < childs.length; i++){
			if(keysize.contains(childs[i])) continue;
			if(type==TYPE_FULL){
				x=0;
				y=0;
				h=parent.getHeight();
				w=parent.getWidth();
			}else if(type==TYPE_VERTICAL){
				h=childs[i].getHeight();
				w=parent.getWidth();
				x=0;
				y+=(i==0)?0:(childs[i-1].getHeight());
			}else if(type==TYPE_HORIZAL){
				h=parent.getHeight();
				w=childs[i].getWidth()==0?generalPerWidth:childs[i].getWidth();
				x+=(i==0)?0:(childs[i-1].getWidth());
				y=0;
			}
			childs[i].setBounds(x, y, w, h);
			if(childs[i] instanceof JScrollPane) childs[i].setSize(w, h);
		}
		
		for (Iterator iterator = keysize.iterator(); iterator.hasNext();) {
			Component c = (Component) iterator.next();
			x=c.getX();
			y=c.getY();
			w=c.getWidth();
			h=c.getHeight();
			c.setBounds(x,y,w,h);
			if(c instanceof JScrollPane) c.setSize(w, h);
		}
	}

	/* (non-Javadoc)
	 * @see org.nobject.common.swing.g.GLayout#minimumLayoutSize(java.awt.Container)
	 */
	public Dimension minimumLayoutSize(Container parent) {
		return parent.getSize();
	}

	/* (non-Javadoc)
	 * @see org.nobject.common.swing.g.GLayout#preferredLayoutSize(java.awt.Container)
	 */
	public Dimension preferredLayoutSize(Container parent) {
		return parent.getSize();
	}

	/* (non-Javadoc)
	 * @see org.nobject.common.swing.g.GLayout#removeLayoutComponent(java.awt.Component)
	 */
	public void removeLayoutComponent(Component comp) {
		
	}

}
